<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/16
 * Time: 17:01
 */

namespace App\Transformers;

use App\Models\UserBase;
use App\Models\UserGroupMember;
use Illuminate\Support\Str;
use League\Fractal\TransformerAbstract;

class UserGroupMemberTransformer extends TransformerAbstract
{


    public function transform(UserGroupMember $userGroupMember)
    {

        $user = $this->getUser($userGroupMember);
        //  群用户信息
        return [
            'id' => $userGroupMember->id,
            'user_account' => $userGroupMember->user_account,
            'user_name' => $user->name,
            'pic' => $this->getPic($user),
            'created_at' => $userGroupMember->created_at->toDateTimeString(),
        ];
    }


    public function getPic(UserBase $user)
    {
        if($user->type == 1){
            $pic       =  'http://www.365tang.cn/upload/face/user_man.png';
            if($user->patient){
                if($user->patient->PIC){
                    if (!Str::startsWith($user->patient->PIC, 'http')) {
                        $pic  = 'http://www.365tang.cn/upload/face/' . $user->patient->PIC;
                    }else{
                        $pic       =  $user->patient->PIC;
                    }
                }
            }

        }else{

            if($user->sex == 1){
                $pic       =  'http://www.365tang.cn/upload/public/doctor_female.png';
            }else{
                $pic       =  'http://www.365tang.cn/upload/public/doctor_male.png';
            }

            if($user->detail){
                if($user->detail->PIC){
                    if (!Str::startsWith($user->detail->PIC, 'http')) {
                        $pic   =   'http://www.365tang.cn/upload/'.$user->detail->PIC;
                    }else{
                        $pic       =  $user->detail->PIC;
                    }

                }
            }

        }
        return $pic;

    }

    public function getUser(UserGroupMember $userGroupMember)
    {

        $str = strstr( $userGroupMember->user_account, '_');
        $user_account =  substr($str,1);
        $user = UserBase::where('account',$user_account)->first();

        return $user;
    }
}